<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 出库日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">出库日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 爱心码 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/loveCode.png" />
          <span class="li_txt">爱心码</span>
        </div>
        <div class="label">
          <input type="number" @blur="Mouse_out" v-model="donateId" />
        </div>
      </li>
      <!-- 出库单号 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/symbol.png" />
          <span class="li_txt">出库单号</span>
        </div>
        <div class="label">
          <input type="number" :value="outId" :disabled="true" />
        </div>
      </li>
      <!-- 快递单号 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/symbol.png" />
          <span class="li_txt">快递单号</span>
        </div>
        <div class="label">
          <input type="number" v-model="shipId" />
        </div>
      </li>
      <!-- 收货方 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/recipient.png" />
          <span class="li_txt">收货方</span>
        </div>
        <div class="label">
          <input type="text" :value="donatee" :disabled="true" />
        </div>
      </li>
      <!-- 运输车牌号 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/transport.png" />
          <span class="li_txt">运输车牌号</span>
        </div>
        <div class="label">
          <input type="text" v-model="plateNo" />
        </div>
      </li>
      <!-- 操作者 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/donation.png" />
          <span class="li_txt">操作者</span>
        </div>
        <div class="label">
          <input type="text" v-model="operator" />
        </div>
      </li>
      <!-- 确认按钮 -->
      <button class="btn" @click="btnFun">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "LogisticsCompany",
  data() {
    return {
      userDate: "", // 展示给用户
      outTime: "",
      donateId: "",
      outId: "",
      shipId: "",
      donatee: "",
      plateNo: "",
      operator: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.outTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
  },
  methods: {
    // 根据出库单号自动填写内容
    Mouse_out() {
      if (!this.donateId) {
        this.donatee = "";
        this.outId = "";
      } else if (this.donateId != null) {
        let loveCode = {
          userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
          token: JSON.parse(localStorage.getItem("login")).res.token,
          donateId: this.donateId
        };
        //通过爱心码调用物资出库信息;
        this.$api
          .getProductOutById(loveCode)
          .then(res => {
            this.donatee = res[0].donatee;
            this.outId = res[0].outId;
          })
          .catch(res => {
            this.$dialog.alert({
              message: "未查询到该爱心码"
            });
            this.donatee = "";
            this.outId = "";
          });
      }
    },
    // 提交
    btnFun() {
      if (!this.donateId) {
        this.$dialog.alert({
          message: "请输入爱心码"
        });
      } else {
        this.loading = true; // loading显示
        let params = {
          token: JSON.parse(localStorage.getItem("login")).res.token,
          userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
          outTime: this.outTime,
          outId: this.outId,
          donateId: this.donateId,
          shipId: this.shipId,
          donatee: this.donatee,
          plateNo: this.plateNo,
          operator: this.operator
        };
        this.$api
          .requestshippingProduct(params)
          .then(res => {
            this.loading = false; // loading隐藏
            this.$dialog.alert({
              message: res
            });
          })
          .catch(err => {
            this.loading = false; // loading隐藏
            this.$dialog.alert({
              message: err
            });
          });
      }
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>